package com.lightkits.wms.util;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import com.lightkits.common.db.entity.syscode.SysCodeBase;
import com.lightkits.common.db.entity.syscode.SysCodeTl;
import com.lightkits.common.db.entity.syscode.SysCodeValueBase;
import com.lightkits.common.db.entity.syscode.SysCodeValueTl;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.common.utility.object.StringUtil;

import java.util.*;
import java.util.stream.Collectors;

/**
 * DomainUtil
 *
 * @author zhuangwei.zheng@hand-china.com
 * Created on 2018/10/17 11:37
 */
public class DomainUtil {
    //根据sysCode获取所有的codevalue和meaning
    public static Map<String, String> getSysCodeMeaning(String sysCode) {
        Map<String, String> result = new HashMap<>();
        SysCodeBase sysCodeBase = new SysCodeBase().selectOne(new EntityWrapper()
                .eq(SysCodeBase.CODE, sysCode));
        if (null != sysCodeBase) {
            List<SysCodeValueBase> sysCodeValueBases = new SysCodeValueBase().selectList(new EntityWrapper()
                    .eq(SysCodeValueBase.CODE_ID, sysCodeBase.getId()));

            List<String> codeValuesIds = sysCodeValueBases
                    .stream().map(target ->
                            target.getId().toString()).collect(Collectors.toList());

            List<SysCodeValueTl> sysCodeValueTls = new SysCodeValueTl().selectList(new EntityWrapper<SysCodeValueTl>()
                    .in(SysCodeValueTl.CODE_VALUE_ID, codeValuesIds));

            sysCodeValueBases.forEach(sysCodeValueBase ->
                    sysCodeValueTls.forEach(sysCodeValueTl -> {
                        if (sysCodeValueTl.getCodeValueId().equals(sysCodeValueBase.getId())) {
                            result.put(sysCodeValueBase.getCodeValue(), sysCodeValueTl.getMeaning());
                        }
                    })
            );
        }
        return result;
    }

    //根据sysCode获取所有code_value_tl表的实体对象
    public static List<SysCodeValueDTO> getCodeValueDTOs(String sysCode) {
        SysCodeBase sysCodeBase = new SysCodeBase()
                .selectOne(new EntityWrapper<SysCodeBase>()
                        .eq(SysCodeBase.CODE, sysCode));
        if (null == sysCodeBase) {
            return Collections.emptyList();
        }
        SysCodeTl sysCodeTl = new SysCodeTl()
                .selectOne(new EntityWrapper<SysCodeTl>()
                        .eq(SysCodeTl.CODE_ID, sysCodeBase.getId()));

        List<SysCodeValueBase> codeValueBases = new SysCodeValueBase()
                .selectList(new EntityWrapper<SysCodeValueBase>()
                        .eq(SysCodeValueBase.CODE_ID, sysCodeBase.getId()));
        if (ObjectUtil.isNullOrEmpty(codeValueBases)) {
            return Collections.emptyList();
        }

        List<Long> codeValueIds = codeValueBases
                .stream()
                .map(SysCodeValueBase::getId)
                .collect(Collectors.toList());

        List<SysCodeValueTl> codeValueTls = new SysCodeValueTl()
                .selectList(new EntityWrapper<SysCodeValueTl>()
                        .in(SysCodeValueTl.CODE_VALUE_ID, codeValueIds));
        return codeValueBases
                .stream()
                .map(base -> new SysCodeValueDTO() {
                    {
                        if (null != sysCodeTl) {
                            setCodeDesc(sysCodeTl.getDescription());
                            setName(sysCodeTl.getCodeName());
                        }
                        setAdditionCode(base.getAdditionCode());
                        setCodeId(base.getCodeId().toString());
                        setValueId(base.getId().toString());
                        setEnabledFlag(base.getEnabledFlag());
                        setValue(base.getCodeValue());
                        Optional<SysCodeValueTl> opt = codeValueTls
                                .stream()
                                .filter(target -> base.getId().toString().equals(target.getCodeValueId().toString()))
                                .findFirst();
                        opt.ifPresent(target -> {
                            setValueTlId(target.getId().toString());
                            setMeaning(target.getMeaning());
                            setLanguageCode(target.getLanguageCode());
                            setValueDesc(target.getDescription());
                        });
                    }
                }).collect(Collectors.toList());
    }

    //将字符串转换为long，如果为空，则返回null;
    public static Long parseLong(String s) {
        return StringUtil.isNullOrEmptyOrWhitespace(s) ? null : Long.parseLong(s);
    }

    //将long转换为String,若果为空,则返回null
    public static String parseString(Long l) {
        return l == null ? null : l.toString();
    }

    //判断字符串A是否包含字符串B  若果A为空则返回fasle,如果B为空，返回true
    public static boolean isContain(String A, String B) {
        if (null == A || "".equals(A)) {
            return false;
        }
        if (null == B || "".equals(B)) {
            return true;
        }
        return A.contains(B);
    }

    //判断一个字符串是否非负整数
    public static boolean isNumber(String str) {
        if (null == str || str.length() <= 0 || str.length() > 10) {
            return false;
        }
        char c = str.charAt(0);
        if (c > '9' || c < '1') {
            return false;
        }
        for (int i = 1; i < str.length(); i++) {
            if (str.charAt(i) > '9' || str.charAt(i) < '0') {
                return false;
            }
        }
        return true;
    }

    //获取当天开始时间
    public static Date getDayStart() {
        Calendar dayStart = Calendar.getInstance();
        dayStart.set(Calendar.HOUR_OF_DAY, 0);
        dayStart.set(Calendar.MINUTE, 0);
        dayStart.set(Calendar.SECOND, 0);
        dayStart.set(Calendar.MILLISECOND, 0);
        return dayStart.getTime();
    }

    //获取当天结束时间
    public static Date getDayEnd() {
        Calendar dayEnd = Calendar.getInstance();
        dayEnd.set(Calendar.HOUR_OF_DAY, 23);
        dayEnd.set(Calendar.MINUTE, 59);
        dayEnd.set(Calendar.SECOND, 59);
        dayEnd.set(Calendar.MILLISECOND, 999);
        return dayEnd.getTime();
    }

    public static String delMoreZero(String s) {
        if (StringUtil.isNullOrEmptyOrWhitespace(s)) {
            return s;
        }
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");
            s = s.replaceAll("[.]$", "");
        }
        return s;
    }

    public static boolean convertFlag(Boolean flag) {
        if (ObjectUtil.isEmpty(flag)) {
            return false;
        }
        return flag;
    }

}
